package com.cmc.cloud.cmclink.doc.enums.filter.util;

import cn.hutool.core.collection.CollectionUtil;
import com.cmc.cloud.cmclink.doc.vo.FilterConditionVO;
import com.cmc.cloud.cmclink.doc.vo.FilterFieldVO;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 周贤
 * @date 2025/6/19
 * 获取枚举过滤字段的枚举实例
 */
public class FilterEnumUtil {

    private static final String PACKAGE_URL = "com.cmc.cloud.cmclink.doc.enums.filter.";

    private static final String CONDITION_PACKAGE_URL = "com.cmc.cloud.cmclink.common.filter.enums.FilterCondition";

    public static List<FilterFieldVO> getFilterInfo(String enumType, String keyWord) {
        List<FilterFieldVO> filterFieldVos = CollectionUtil.newArrayList();
        try {
            Class<?> filterClass = Class.forName(PACKAGE_URL + enumType);
            Enum[] enumConstants = (Enum[]) filterClass.getEnumConstants();
            for (Enum enumConstant : enumConstants) {
                FilterFieldVO filterFieldVO = new FilterFieldVO();
                Method methodN = filterClass.getMethod("getFieldName");
                String fieldName = (String) methodN.invoke(enumConstant);
                filterFieldVO.setFieldName(fieldName);

                Method methodT = filterClass.getMethod("getFieldType");
                String fieldType = (String) methodT.invoke(enumConstant);
                filterFieldVO.setFieldType(fieldType);

                Method methodD = filterClass.getMethod("getFieldDesc");
                String fieldDesc = (String) methodD.invoke(enumConstant);
                filterFieldVO.setFieldDesc(fieldDesc);
                filterFieldVos.add(filterFieldVO);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        if (StringUtils.isNotEmpty(keyWord)) {
            return filterFieldVos.stream().filter(vo -> vo.getFieldDesc().contains(keyWord)).collect(Collectors.toList());
        }
        return filterFieldVos;
    }

    public static List<FilterConditionVO> getFilterConditionInfo(String keyWord) {
        List<FilterConditionVO> conditionVos = CollectionUtil.newArrayList();
        try {
            Class<?> filterClass = Class.forName(CONDITION_PACKAGE_URL);
            Enum[] enumConstants = (Enum[]) filterClass.getEnumConstants();
            for (Enum enumConstant : enumConstants) {
                FilterConditionVO conditionVO = new FilterConditionVO();
                Method methodN = filterClass.getMethod("getDescription");
                String description = (String) methodN.invoke(enumConstant);
                conditionVO.setDescription(description);

                Method methodT = filterClass.getMethod("getDescriptionEn");
                String descriptionEn = (String) methodT.invoke(enumConstant);
                conditionVO.setDescriptionEn(descriptionEn);

                Method methodD = filterClass.getMethod("getOperator");
                String operate = (String) methodD.invoke(enumConstant);
                conditionVO.setOperator(operate);
                conditionVO.setOperatorName(enumConstant.name());

                conditionVos.add(conditionVO);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        if (StringUtils.isNotEmpty(keyWord)) {
            return conditionVos.stream().filter(vo -> vo.getDescription().contains(keyWord)).collect(Collectors.toList());
        }
        return conditionVos;
    }
}
